package com.xxr.mom.ci.charging.interworking.base;

import com.xxr.mom.ci.charging.interworking.enums.VersionEnum;
import com.xxr.mom.ci.common.dto.EncryptionDecryptionDto;
import com.xxr.mom.ci.common.dto.InterworkingParam;
import com.xxr.mom.ci.common.util.EncryptionDecryptionUtil;
import com.xxr.mom.ci.common.util.ReflectionUtil;

/**
 * @description: InterworkingInterface
 * @date: 2024/8/27 00:02
 * @author: natsuki_kining
 * @version: 1.0
 */
public interface InterworkingInterface<R, P> {

    /**
     * 接口版本
     * @return
     */
    VersionEnum version();

    /**
     * 解密报文为请求参数类型
     *
     * @param decodeDto
     * @param encryptData
     * @return
     */
    default R decodeToR(EncryptionDecryptionDto decodeDto, String encryptData) {
        Class<?> requestParamType = ReflectionUtil.findSuperClassParameterType(this, 0);
        return (R) EncryptionDecryptionUtil.decodeObject(decodeDto, encryptData, requestParamType);
    }

    /**
     * 解密报文为响应参数类型
     * @param decodeDto
     * @param encryptData
     * @return
     */
    default R decodeToP(EncryptionDecryptionDto decodeDto, String encryptData) {
        Class<?> requestParamType = ReflectionUtil.findSuperClassParameterType(this, 1);
        return (R) EncryptionDecryptionUtil.decodeObject(decodeDto, encryptData, requestParamType);
    }

    /**
     * 校验参数
     *
     * @param r 请求参数
     */
    default void validateRequestParam(R r) {

    }

    /**
     * 获取签名
     * @param interworkingParam InterworkingParam
     * @param sigSecret 签名密钥
     * @return 签名
     */
    default String getSign(InterworkingParam interworkingParam, String sigSecret) {
        return EncryptionDecryptionUtil.sign(sigSecret, interworkingParam.getOperatorId() + interworkingParam.getData() + interworkingParam.getTimeStamp() + interworkingParam.getSeq());
    }
}
